﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace postSale.forms
{
    public partial class processWorkEdit : processEdit
    {
        public processWorkEdit()
        {
            try
            {
                InitializeComponent();
                processWorkDetail.myTaskSource = tasksSource;
                this.myValidTaskStatusList = new common.CommonStatus[] { common.CommonStatus.Processing, common.CommonStatus.Completed};
                this.myValidTaskTypeList = new myTypes.taskType[] { myTypes.taskType.User_Immediate,myTypes.taskType.Auto };
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        public override void LockEdit()
        {
            base.LockEdit();
            bool lockState = this.isLockEdit;
            cbStatus.Enabled = false;
            processWorkDetail.ReadOnly = lockState;
            workAddBtn.Enabled = !lockState;
            workDelBtn.Enabled = !lockState;
            workFinishBtn.Enabled = !lockState; 
        }
        protected override bool UpdateData(DataRow row)
        {
            //base.UpdateData(row);
            processWorkDetail.SaveData();
            return true;
        }
        protected override void CancelAllChanges()
        {
            myPostSalesDS.tasks.RejectChanges();
            base.CancelAllChanges();
        }
        protected override bool DataChanged()
        {
            if (base.DataChanged()) return true;
            DataTable changeTbl = myPostSalesDS.taskWork.GetChanges();
            return (changeTbl != null && changeTbl.Rows.Count > 0);
        }
        protected override bool DataValid(bool showMsg)
        {
            if (!base.DataValid(showMsg)) return false;
            if (cbStatus.myValue != common.CommonStatus.Processing)
            {
                common.SysLibs.ShowErrorMessage("Không thể thay đổi dữ liệu này.");
                return false;
            }
            return true;
        }
      
        private void workAddBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                processWorkDetail.AddRow();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void workDelBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                processWorkDetail.RemoveRow();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void workFinishBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                if (!DataValid(true)) return;

                if (tasksSource.Current == null) return;
                if (MessageBox.Show("Kết thúc công việc này ?", common.Settings.sysApplicationName, MessageBoxButtons.YesNo) == DialogResult.No) return;
                data.postSaleDS.tasksRow taskRow = (data.postSaleDS.tasksRow)((DataRowView)tasksSource.Current).Row;
                cbStatus.myValue = common.CommonStatus.Completed;
                taskRow.status = (byte)common.CommonStatus.Completed;
                taskRow.closeDate = DateTime.Now;
                tasksSource.EndEdit();
                taskTA.Update(taskRow);
                taskRow.AcceptChanges();
                UpdateData(taskRow);
                this.ShowMessage("Đã lưu dữ liệu.");
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void tasksSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                processWorkDetail.LoadData();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        private void processWorkEdit_Load(object sender, EventArgs e)
        {
            try
            {
                LockEditData(true);
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
    }
}
